<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" draggable :show-fullscreen="true" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
            <el-form-item label="订单类型" prop="type">
                <el-radio v-model="form.type" v-for="(item,index) in type_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="订单号" prop="order_sn">
                <el-input v-model="form.order_sn" clearable></el-input>
        	</el-form-item>
            <el-form-item label="用户" prop="user_id">
                <el-input-number v-model="form.user_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="活动类型" prop="activity_type">
                <el-switch v-model="form.activity_type" :active-value="1" :inactive-value="0"></el-switch>
        	</el-form-item>
            <el-form-item label="商品总价" prop="goods_amount">
                <el-input-number v-model="form.goods_amount"></el-input-number>
        	</el-form-item>
            <el-form-item label="总运费" prop="dispatch_amount">
                <el-input-number v-model="form.dispatch_amount"></el-input-number>
        	</el-form-item>
            <el-form-item label="联系电话" prop="phone">
                <el-input v-model="form.phone" clearable></el-input>
        	</el-form-item>
            <el-form-item label="收货人" prop="consignee">
                <el-input v-model="form.consignee" clearable></el-input>
        	</el-form-item>
            <el-form-item label="省" prop="province_name">
                <el-input v-model="form.province_name" clearable></el-input>
        	</el-form-item>
            <el-form-item label="市" prop="city_name">
                <el-input v-model="form.city_name" clearable></el-input>
        	</el-form-item>
            <el-form-item label="区" prop="area_name">
                <el-input v-model="form.area_name" clearable></el-input>
        	</el-form-item>
            <el-form-item label="乡镇" prop="town_name">
                <el-input v-model="form.town_name" clearable></el-input>
        	</el-form-item>
            <el-form-item label="详细地址" prop="address">
                <el-input v-model="form.address" clearable></el-input>
        	</el-form-item>
            <el-form-item label="省ID" prop="province_id">
                <el-input-number v-model="form.province_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="市ID" prop="city_id">
                <el-input-number v-model="form.city_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="区ID" prop="area_id">
                <el-input-number v-model="form.area_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="镇ID" prop="town_id">
                <el-input-number v-model="form.town_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="订单状态" prop="status">
                <el-radio v-model="form.status" v-for="(item,index) in status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="发票开具状态" prop="invoice_status">
                <el-radio v-model="form.invoice_status" v-for="(item,index) in invoice_status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="商户备注" prop="memo">
                <el-input v-model="form.memo" clearable></el-input>
        	</el-form-item>
            <el-form-item label="用户备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
        	</el-form-item>
            <el-form-item label="订单总金额" prop="total_amount">
                <el-input-number v-model="form.total_amount"></el-input-number>
        	</el-form-item>
            <el-form-item label="积分总数" prop="score_amount">
                <el-input-number v-model="form.score_amount"></el-input-number>
        	</el-form-item>
            <el-form-item label="支付金额" prop="total_fee">
                <el-input-number v-model="form.total_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="改价前金额" prop="last_total_fee">
                <el-input-number v-model="form.last_total_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="折扣总金额" prop="discount_fee">
                <el-input-number v-model="form.discount_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="平台优惠券抵用金额" prop="coupon_fee">
                <el-input-number v-model="form.coupon_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="店铺优惠券抵用金额" prop="store_coupon_fee">
                <el-input-number v-model="form.store_coupon_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="活动优惠" prop="activity_discount_money">
                <el-input-number v-model="form.activity_discount_money"></el-input-number>
        	</el-form-item>
            <el-form-item label="运费优惠" prop="dispatch_discount_money">
                <el-input-number v-model="form.dispatch_discount_money"></el-input-number>
        	</el-form-item>
            <el-form-item label="实际支付金额" prop="pay_fee">
                <el-input-number v-model="form.pay_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="支付贝壳总数" prop="shell_fee">
                <el-input-number v-model="form.shell_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="积分支付数" prop="score_fee">
                <el-input-number v-model="form.score_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="积分服务费金额" prop="score_money_fee">
                <el-input-number v-model="form.score_money_fee"></el-input-number>
        	</el-form-item>
            <el-form-item label="商品原价" prop="goods_original_amount">
                <el-input-number v-model="form.goods_original_amount"></el-input-number>
        	</el-form-item>
            <el-form-item label="优惠券 id" prop="coupons_id">
                <el-input-number v-model="form.coupons_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="交易单号" prop="transaction_id">
                <el-input v-model="form.transaction_id" clearable></el-input>
        	</el-form-item>
            <el-form-item label="交易原始数据" prop="payment_json">
                <el-input v-model="form.payment_json" clearable></el-input>
        	</el-form-item>
            <el-form-item label="支付方式" prop="pay_type">
                <el-radio v-model="form.pay_type" v-for="(item,index) in pay_type_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="支付时间" prop="paytime">
                <el-date-picker v-model="form.paytime" type="datetime"></el-date-picker>
        	</el-form-item>
            <el-form-item label="附加字段" prop="ext">
                <el-input v-model="form.ext" clearable></el-input>
        	</el-form-item>
            <el-form-item label="平台" prop="platform">
                <el-select v-model="form.platform" clearable placeholder="请选择平台">
            <el-option v-for="(item,index) in platform_option" :key="index" :label="item" :value="index"></el-option>
        </el-select>
        	</el-form-item>
            <el-form-item label="店铺ID" prop="store_id">
                <el-input-number v-model="form.store_id"></el-input-number>
        	</el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='read'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    
    export default {
        emits: ['success', 'closed'],
        
        data() {
            return {
                mode: "create",
                titleMap: {
                    read: '查看',
                    create: '新增',
                    update: '编辑',
                },
                visible: false,
                isSaveing: false,
                form: {
                    id: 0,
					type: '',
					order_sn: '',
					user_id: '',
					activity_type: '',
					goods_amount: '',
					dispatch_amount: '',
					phone: '',
					consignee: '',
					province_name: '',
					city_name: '',
					area_name: '',
					town_name: '',
					address: '',
					province_id: '',
					city_id: '',
					area_id: '',
					town_id: '',
					status: '',
					invoice_status: '',
					memo: '',
					remark: '',
					total_amount: '',
					score_amount: '',
					total_fee: '',
					last_total_fee: '',
					discount_fee: '',
					coupon_fee: '',
					store_coupon_fee: '',
					activity_discount_money: '',
					dispatch_discount_money: '',
					pay_fee: '',
					shell_fee: '',
					score_fee: '',
					score_money_fee: '',
					goods_original_amount: '',
					coupons_id: '',
					transaction_id: '',
					payment_json: '',
					pay_type: '',
					paytime: '',
					ext: '',
					platform: '',
					store_id: '',
                },
                rules: {
                },
				type_option:{"goods":"商城订单","score":"积分商城订单"},
				activity_type_option:{"0":"关闭","1":"开启"},
				status_option:{"-2":"交易关闭","-1":"已取消","0":"未支付","1":"已支付","2":"已完成"},
				invoice_status_option:{"-1":"不可开具","0":"未申请","1":"已申请"},
				pay_type_option:{"wechat":"微信支付","alipay":"支付宝","wallet":"钱包支付","score":"积分支付","shell":"贝壳支付","mix":"混合支付"},
				platform_option:{"H5":"H5","wxOfficialAccount":"微信公众号","wxMiniProgram":"微信小程序","App":"App"},
            }
        },
        methods: {
            open(mode='create'){
                this.mode = mode;
                this.visible = true;
                return this
            },
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = await this.$API.order[this.mode].post(this.form);
                        this.isSaveing = false;
                        if(res.code == 200){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success(res.message)
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }else{
                        return false;
                    }
                })
            },
            setData(data){
                this.$TABLE.assign(this,data);
            }
        }
    }
</script>